package com.webchat.client.mall.controller;

import com.webchat.client.mall.service.MallProductService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.mall.MallProductOperateVO;
import com.webchat.domain.vo.request.mall.MallSPURequestVO;
import com.webchat.domain.vo.response.mall.MallSPUDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/mall-service/product")
public class MallProductController {

    @Autowired
    private MallProductService mallProductService;

    /**
     * 商家商品配置
     *
     * @param spuRequest
     * @return
     */
    @ValidateLogin
    @PostMapping("/save")
    public APIResponseBean<Long> save(@RequestBody MallSPURequestVO spuRequest) {

        spuRequest.setOperator(SessionHelper.getCurrentUserId());
        Long productId = mallProductService.save(spuRequest);
        return APIResponseBeanUtil.success(productId);
    }


    /**
     * 商品上架
     *
     * @return
     */
    @ValidateLogin
    @PostMapping("/publish/{productId}")
    public APIResponseBean<Boolean> publish(@PathVariable Long productId) {

        String userId = SessionHelper.getCurrentUserId();
        Boolean res = mallProductService.publish(new MallProductOperateVO(productId, userId));
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 商品撤回
     *
     * @param productId
     * @return
     */
    @ValidateLogin
    @PostMapping("/invoke/{productId}")
    public APIResponseBean<Boolean> invoke(@PathVariable Long productId) {

        String userId = SessionHelper.getCurrentUserId();
        Boolean res = mallProductService.invoke(new MallProductOperateVO(productId, userId));
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 商品删除
     *
     * @param productId
     * @return
     */
    @ValidateLogin
    @PostMapping("/delete")
    public APIResponseBean<Boolean> delete(@PathVariable Long productId) {

        String userId = SessionHelper.getCurrentUserId();
        Boolean res = mallProductService.delete(new MallProductOperateVO(productId, userId));
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 商品详情
     *
     * @param productId
     * @return
     */
    @GetMapping("/detail/{productId}")
    public APIResponseBean<MallSPUDetailVO> detail(@PathVariable Long productId) {

        String userId = SessionHelper.getCurrentUserId();
        MallSPUDetailVO product = mallProductService.detail(productId, userId);
        return APIResponseBeanUtil.success(product);
    }

}
